package com.jthinker.blog.web.controller.front.post;

import com.jthinker.blog.bean.entity.Category;
import com.jthinker.blog.bean.entity.Post;
import com.jthinker.blog.bean.enu.PostStatus;
import com.jthinker.blog.bean.ex.ResourceNotFoundException;
import com.jthinker.blog.bean.vo.PostFilter;
import com.jthinker.blog.bean.vo.QueryResult;
import com.jthinker.blog.service.CategoryService;
import com.jthinker.blog.service.PostService;
import com.jthinker.blog.web.controller.admin.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * User: Gang Chen
 * Date: 2015/8/2 20:04
 */
@Controller
public class PostController extends BaseController
{
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private PostService postService;

    @ModelAttribute("categorys")
    public List<Category> categorys()
    {
        return categoryService.listAll();
    }

    @ModelAttribute("recentList")
    public List<Post> recentList()
    {
        return postService.queryRecentList(3);
    }

    @ModelAttribute("archives")
    public List<Object[]> archives()
    {
        return postService.queryArchives();
    }

    @RequestMapping("/")
    public ModelAndView view(PostFilter filter)
    {
        return postList(filter);
    }

    @RequestMapping(value = "/post/category/{slug}", method = RequestMethod.GET)
    public ModelAndView category(@PathVariable("slug") String slug, PostFilter filter)
    {
        Long categoryId = categoryService.getIdBySlug(slug);
        if (categoryId == null)
        {
            throw new ResourceNotFoundException();
        }
        filter.setCategoryId(categoryId);

        return postList(filter);
    }

    @RequestMapping(value = "/post/date/{year}", method = RequestMethod.GET)
    public ModelAndView date(@PathVariable("year") Integer year, PostFilter filter)
    {
        return date(year, null, filter);
    }

    @RequestMapping(value = "/post/date/{year}/{month}", method = RequestMethod.GET)
    public ModelAndView date(@PathVariable("year") Integer year, @PathVariable("month") Integer month, PostFilter filter)
    {
        if (year == null)
        {
            throw new ResourceNotFoundException();
        }

        filter.setYear(year);
        filter.setMonth(month);

        return postList(filter);
    }

    @RequestMapping(value = "/post/user/{userId}", method = RequestMethod.GET)
    public ModelAndView user(@PathVariable("userId") Long userId, PostFilter filter)
    {
        if (userId == null)
        {
            throw new ResourceNotFoundException();
        }

        filter.setUserId(userId);
        return postList(filter);
    }

    public ModelAndView postList(PostFilter filter)
    {
        ModelAndView mv = new ModelAndView("index/index");
        QueryResult<Post> result = postService.queryPagination(filter);
        mv.addObject("result", result);
        return mv;
    }

    @RequestMapping(value = "/post/{id}", method = RequestMethod.GET)
    public ModelAndView postView(@PathVariable("id") Long id)
    {
        if (id == null)
        {
            throw new ResourceNotFoundException();
        }

        Post post = postService.readDetail(id);
        if (post == null || post.getStatus() != PostStatus.PUBLISH)
        {
            throw new ResourceNotFoundException();
        }

        ModelAndView mv = new ModelAndView("post/postView");
        mv.addObject("post", post);
        return mv;
    }

    @RequestMapping(value = "/about", method = RequestMethod.GET)
    public String about()
    {
        return "about/about";
    }

    @RequestMapping(value = "/contact", method = RequestMethod.GET)
    public String contact()
    {
        return "contact/contact";
    }
}
